define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        // 義工列表
        indexsss: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteer/indexsss/ids/'+Config.ids + location.search,
                    add_url: 'volunteer/add',
                    edit_url: 'volunteer/edit',
                    del_url: 'volunteer/del',
                    multi_url: 'volunteer/multi',
                    import_url: 'volunteer/import',
                    table: 'volunteer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                singleSelect: true, //是否启用单选
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'volunteerspecialty.name', title: __('Volunteerspecialty.name'), operate: false},
                        {field: 'service_time_type', title: __('Service_time_type'), operate: false, searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 启动和暂停按钮
            $(document).on("click", ".btn-start", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                if ( !ids.join(",") ) {
                      Toastr.error('請選擇一項');
                      return false;
                }
                Fast.api.ajax({
                url:$('.btn-start').attr('data-url'),
                data:{volunteer_id:ids.join(","),action:'select'},
                loading:false,
                 success: function (data, ret) {
                    if ( data.code == 1 ) {
                        Toastr.success(data.msg);
                        setTimeout('Fast.api.close()', 1500);
                    }else{

                      Toastr.error(data.msg);
                     //这里ret.msg 是未定义的。 data里的数据需要 data.data.*来使用
                    }
                 }
                 });
                // Table.api.multi("", ids.join(","), table, this);
                // Fast.api.close();//这里是重点



                // Layer.close('操作成功');
            });

        },
        // 分配義工
        indexs: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteer/indexs/source/'+Config.source + location.search,
                    add_url: 'volunteer/add',
                    edit_url: 'volunteer/edit',
                    del_url: 'volunteer/del',
                    multi_url: 'volunteer/multi',
                    import_url: 'volunteer/import',
                    table: 'volunteer',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'volunteerspecialty.name', title: __('Volunteerspecialty.name'), operate: false},
                        {field: 'service_time_type', title: __('Service_time_type'), operate: false, searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                // data = JSON.stringify(Table.api.selecteddata(table));
                data = Table.api.selecteddata(table);
                // Layer.alert();
                Fast.api.close(data);//这里是重点
            });
        },
        add: function () {
            Controller.api.bindevent();
        },
        audit: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});